﻿using DBQLTV;
using System;
using System.Collections.Generic;
using System.Linq;

namespace DAO
{
    public class DocGiaDAO
    {
        public bool InsertDocGia(string HoTen, string DiaChi, LOAIDOCGIA LoaiDG, string Email, string NgaySinh, string NgayLapThe, string NgayHetHanThe)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                DateTime ngSinh = Convert.ToDateTime(NgaySinh);
                DOCGIA docgia_temp = entity.DOCGIAs.FirstOrDefault(o => o.Ten == HoTen && o.NgaySinh == ngSinh && o.DiaChi == DiaChi);
                if (docgia_temp != null)
                    return false;

                DOCGIA docgia = new DOCGIA()
                {
                    Ten = HoTen,
                    DiaChi = DiaChi,
                    MaLoaiDG = LoaiDG.MaLoaiDG,
                    Email = Email,
                    NgaySinh = Convert.ToDateTime(NgaySinh),
                    NgayLapThe = Convert.ToDateTime(NgayLapThe),
                    NgayHetHanThe = Convert.ToDateTime(NgayHetHanThe),
                    TongNo = 0
                };

                entity.DOCGIAs.AddObject(docgia);
                entity.SaveChanges();
                return true;
            }
        }
        public bool DeleteDocGia(int MaDocGia)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                DOCGIA docgia = entity.DOCGIAs.FirstOrDefault(o => o.MaDG == MaDocGia);

                if (docgia != null)
                {
                    entity.DOCGIAs.DeleteObject(docgia);
                    entity.SaveChanges();
                    return true;
                }
                return false;
            }
        }
        public bool UpdateTongNo(int MaDocGia, int TongNo)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                DOCGIA docgia = entity.DOCGIAs.FirstOrDefault(o => o.MaDG == MaDocGia);
                if (docgia == null)
                    return false;
                docgia.TongNo += TongNo;
                entity.SaveChanges();
                return true;
            }
        }
        public List<DOCGIA> GetListDocGia()
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                List<DOCGIA> lDocGia = entity.DOCGIAs.ToList();
                return lDocGia;
            }
        }
        public DOCGIA GetDocGia(string HoTen, string NgaySinh, string DiaChi)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                DateTime ngSinh = Convert.ToDateTime(NgaySinh);
                DOCGIA docgia_temp = entity.DOCGIAs.FirstOrDefault(o => o.Ten == HoTen && o.NgaySinh == ngSinh && o.DiaChi == DiaChi);
                return docgia_temp;
            }
        }
        public DOCGIA GetDocGia(int MaDG)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                DOCGIA docgia_temp = entity.DOCGIAs.FirstOrDefault(o => o.MaDG == MaDG);
                return docgia_temp;
            }
        }
        public List<int> GetListMaDocGiaByName(string HoTen)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                var listDocGia = from temp in GetListDocGia() where temp.Ten.Equals(HoTen) select temp;
                List<int> result = new List<int>();
                foreach (var item in listDocGia)
                    result.Add(item.MaDG);
                return result;
            }
        }
        public LOAIDOCGIA GetLoaiDocGia(int MaDG)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                DOCGIA docgia_temp = entity.DOCGIAs.FirstOrDefault(o => o.MaDG == MaDG);
                return docgia_temp.LOAIDOCGIA;
            }
        }
    }
}
